
package com.lch.base.http.callback;

import com.lch.base.http.exception.ApiException;

import java.lang.reflect.Type;

/**
 * callback的基础接口
 *
 * @author lee(29487113@qq.com) on 18/2/4
 */
public abstract class ICallbackBase<T, E> {
    protected Object mTag;

    public ICallbackBase() {
    }

    public ICallbackBase(Object tag) {
        mTag = tag;
    }

    //运行在子线程------------------------
    /**
     * 处理返回结果
     */
    public abstract T onHandleResponse(E response) throws Exception;

    /**
     * HTTP层级的状态
     */
    public boolean isHttpReponseOk(E response) throws Exception {
        return true;
    }

    /**
     * 解析数据
     */
    public abstract <T> T transform(E response, Type type) throws Exception;

    /**
     * 解析类型 默认泛型的第一个，basersult需要重写
     * @return
     */
    public abstract Type getTransType();

    //运行在子线程------------------------


    //运行在UI Thread----------------------
    public void onProgress(Object tag, float progress, long transfered, long total) {}
    public void onProgress(Object tag, int progress, long speed, long transfered, long total) {}

    public void onStart() {}
    public void onStart(Object tag) {}

    public void onCancel(Throwable e) {}
    public void onCancel(Object tag, Throwable e) {}

    public void onCompleted() {}
    public void onCompleted(Object tag) {}

    public void onSuccess(T data){};
    public void onFail(ApiException e){}

    public void onSuccess(Object tag, T data) {}
    public void onFail(Object tag, ApiException data) {}

    public void onReset() {}

    //运行在UI Thread----------------------


    public void setTag(Object tag) {
        mTag = tag;
    }

    public Object getTag() {
        return mTag;
    }


    /**
     * 默认callback，没有指定时用这个，防止报错
     */
    public static ICallbackBase CALLBACK_DEFAULT = new ICallbackBase() {
        @Override
        public Object onHandleResponse(Object response) throws Exception {
            return "";
        }

        @Override
        public Object transform(Object response, Type type) throws Exception {
            return null;
        }

        @Override
        public Type getTransType() {
            return null;
        }
    };

}